#include "MemberManager.h"

#include "EventNetwork.h"

#include <libintl.h>
#define _(String) gettext(String)

struct _MemberEntry
{
	AdwActionRow	 parent_instance;
	GtkBox		*suffix_box;
	GtkToggleButton *can_write_toggle;
	AdwAlertDialog	*alert_with_error_dialog;
	//
	euint32 relate_group_id;
	euint32 user_id;
};

G_DEFINE_FINAL_TYPE(MemberEntry, member_entry, ADW_TYPE_ACTION_ROW)

static void
member_entry_set_toggle(MemberEntry *self, bool can_write)
{
	if (can_write) {
		gtk_toggle_button_set_active(self->can_write_toggle, true);
		gtk_button_set_label(GTK_BUTTON(self->can_write_toggle),
				     _("Writable"));
	} else {
		gtk_toggle_button_set_active(self->can_write_toggle, false);
		gtk_button_set_label(GTK_BUTTON(self->can_write_toggle),
				     _("Unwritable"));
	}
}

static void
toggled_writable_cb(MemberEntry *self, GtkToggleButton *toggle)
{
	ResponseType re_type;
	bool	     can_write;

	can_write = gtk_toggle_button_get_active(toggle);
	re_type	  = event_network_modify_user_can_write(
		  event_network, self->relate_group_id, self->user_id, can_write);
	switch (re_type) {
	case RESPONSE_TYPE_SUCCESS:
		member_entry_set_toggle(self, can_write);
		break;
	case RESPONSE_TYPE_NONE:
		adw_alert_dialog_set_body(
			self->alert_with_error_dialog,
			event_network_get_error_info(event_network));
		adw_dialog_present(ADW_DIALOG(self->alert_with_error_dialog),
				   GTK_WIDGET(self));
		break;
	default:
		adw_alert_dialog_set_body(self->alert_with_error_dialog,
					  _(response_type[re_type]));
		adw_dialog_present(ADW_DIALOG(self->alert_with_error_dialog),
				   GTK_WIDGET(self));
		break;
	}
}

static void
alert_error_close_cb(AdwAlertDialog *self, gchar *response, gpointer data)
{
	adw_dialog_close(ADW_DIALOG(self));
}

MemberEntry *
member_entry_new(const char *user_name, euint32 relate_group_id,
		 euint32 user_id, bool can_write)
{
	MemberEntry *object;
	static char  buffer[20];

	object = g_object_new(MEMBER_TYPE_ENTRY, NULL);
	//
	object->user_id		= user_id;
	object->relate_group_id = relate_group_id;
	adw_preferences_row_set_title(ADW_PREFERENCES_ROW(object), user_name);
	sprintf(buffer, "UserID: %07u", user_id);
	adw_action_row_set_subtitle(ADW_ACTION_ROW(object), buffer);
	member_entry_set_toggle(object, can_write);

	return object;
}

static void
member_entry_finalize(GObject *object)
{
	G_OBJECT_CLASS(member_entry_parent_class)->finalize(object);
}

static void
member_entry_class_init(MemberEntryClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	gtk_widget_class_set_template_from_resource(
		widget_class, "/org/korack/event/ui/MemberEntry.ui");
	gtk_widget_class_bind_template_child(widget_class, MemberEntry,
					     suffix_box);
	gtk_widget_class_bind_template_child(widget_class, MemberEntry,
					     can_write_toggle);
	gtk_widget_class_bind_template_child(widget_class, MemberEntry,
					     alert_with_error_dialog);

	gtk_widget_class_bind_template_callback(widget_class,
						toggled_writable_cb);
	gtk_widget_class_bind_template_callback(widget_class,
						alert_error_close_cb);
	//
	object_class->finalize = member_entry_finalize;
}

static void
member_entry_init(MemberEntry *self)
{
	gtk_widget_init_template(GTK_WIDGET(self));
	adw_action_row_add_suffix(ADW_ACTION_ROW(self),
				  GTK_WIDGET(self->suffix_box));
}

struct _MemberManager
{
	AdwDialog parent_instance;
	//
	AdwPreferencesGroup *groups;
	ObjectArray	    *member_array;
	//
	euint32 group_id;
};

G_DEFINE_FINAL_TYPE(MemberManager, member_manager, ADW_TYPE_DIALOG)

void
member_manager_fetch_all_member(MemberManager *self)
{
	GtkWidget *entry;

	self->member_array = event_network_fetch_all_member_info(
		event_network, self->group_id);
	if (!self->member_array) {
		event_debug(MemberManager, member_manager_init, "fetch NULL.");
		return;
	}
	object_array_iteration_reset(self->member_array);

re_iteration:
	entry = GTK_WIDGET(object_array_iteration_get(self->member_array));
	if (!entry)
		return;
	adw_preferences_group_add(self->groups, g_object_ref(entry));
	goto re_iteration;
}

MemberManager *
member_manager_new(euint32 group_id)
{
	MemberManager *object;
	object = g_object_new(MEMBER_TYPE_MANAGER, NULL);
	//
	object->group_id = group_id;
	return object;
}

static void
member_manager_finalize(GObject *object)
{
	MemberManager *self = MEMBER_MANAGER(object);
	event_object_unref(self->member_array);
	G_OBJECT_CLASS(member_manager_parent_class)->finalize(object);
}

static void
member_manager_class_init(MemberManagerClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
	GObjectClass   *object_class = G_OBJECT_CLASS(klass);

	g_type_ensure(MEMBER_TYPE_ENTRY);

	gtk_widget_class_set_template_from_resource(
		widget_class, "/org/korack/event/ui/MemberManager.ui");
	gtk_widget_class_bind_template_child(widget_class, MemberManager,
					     groups);

	object_class->finalize = member_manager_finalize;
}

static void
member_manager_init(MemberManager *self)
{
	gtk_widget_init_template(GTK_WIDGET(self));
}
